package com.happyfamily.springboot.crowdsourcingplatform.controller.wang;

import com.happyfamily.springboot.crowdsourcingplatform.annotation.OperationLogDetail;
import com.happyfamily.springboot.crowdsourcingplatform.base.result.PageTableRequest;
import com.happyfamily.springboot.crowdsourcingplatform.base.result.Results;
import com.happyfamily.springboot.crowdsourcingplatform.enums.OperationType;
import com.happyfamily.springboot.crowdsourcingplatform.enums.OperationUnit;
import com.happyfamily.springboot.crowdsourcingplatform.model.Credit;
import com.happyfamily.springboot.crowdsourcingplatform.model.Parameter;
import com.happyfamily.springboot.crowdsourcingplatform.model.SysUser;
import com.happyfamily.springboot.crowdsourcingplatform.service.wang.CreditService;
import com.happyfamily.springboot.crowdsourcingplatform.service.wang.ParameterService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 参数配置的控制类
 */
@Controller
@RequestMapping("/api-back/parameter")
@Slf4j
public class ParameterController
{
	@Autowired
	private ParameterService parameterServiceImpl;

	/**
	 * 参数查询
	 * @param request
	 * @param parameterType
	 * @param starttime
	 * @param endtime
	 * @return
	 */
	@PreAuthorize("hasAnyAuthority('back:parameter:query')")
	@ApiOperation(value = "参数查询", notes = "参数查询")
	@OperationLogDetail(detail = "参数查询", level = 1, operationUnit = OperationUnit.ADMIN, operationType = OperationType.SELECT)
	@RequestMapping("/searchParameter")
	@ResponseBody
	public Results<Parameter> searchParameter(PageTableRequest request, String parameterType, String starttime, String endtime)
	{
		request.countOffset();
		return parameterServiceImpl.searchParameter(request.getOffset(), request.getLimit(), parameterType, starttime, endtime);
	}

	/**
	 * 查找参数类型下来框
	 * @return
	 */
	@ApiOperation(value = "查找参数类型下来框", notes = "查找参数类型下来框")
	@OperationLogDetail(detail = "查找参数类型下来框", level = 1, operationUnit = OperationUnit.ADMIN, operationType = OperationType.SELECT)
	@GetMapping("/searchType")
	@ResponseBody
	public List<String> selectType()
	{
		return parameterServiceImpl.selectType();
	}

	/**
	 * 参数的增加
	 * @param ptype
	 * @param pname
	 * @return
	 */
	@PreAuthorize("hasAnyAuthority('back:parameter:add')")
	@ApiOperation(value = "参数的增加", notes = "参数的增加")
	@OperationLogDetail(detail = "参数的增加", level = 3, operationUnit = OperationUnit.ADMIN, operationType = OperationType.INSERT)
	@PostMapping("/parameterAdd")
	@ResponseBody
	public String parameterAdd(String ptype, String pname)
	{
		return parameterServiceImpl.parameterAdd(ptype, pname);
	}

	/**
	 * 参数值修改
	 * @param ptype
	 * @param pname
	 * @param pid
	 * @return
	 */
	@PreAuthorize("hasAnyAuthority('back:parameter:updata')")
	@ApiOperation(value = "参数值修改", notes = "参数值修改")
	@OperationLogDetail(detail = "参数值修改", level = 2, operationUnit = OperationUnit.ADMIN, operationType = OperationType.UPDATE)
	@PostMapping("/parameterUpdata")
	@ResponseBody
	public String parameterUpdata(String ptype, String pname, String pid)
	{
		return parameterServiceImpl.parameterUpdata(ptype, pname, pid);
	}

	/**
	 *参数值删除
	 * @param pid
	 * @return
	 */
	@PreAuthorize("hasAnyAuthority('back:parameter:delete')")
	@ApiOperation(value = "参数值删除", notes = "参数值删除")
	@OperationLogDetail(detail = "参数值删除", level = 4, operationUnit = OperationUnit.ADMIN, operationType = OperationType.DELETE)
	@PostMapping("/parameterDelete")
	@ResponseBody
	public String parameterDelete(String pid)
	{
		return parameterServiceImpl.parameterDelete(pid);
	}

}
